using UnityEngine;
using UnityEngine.UI;

public class ButtonController : MonoBehaviour
{
    public GameObject around;
    public GameObject Canvas;
    GameObject back;

    bool animationOn = false;
    float runTime = 0;
    public static float width = 0, height = 0, size_x = 0, size_y = 0;

    public void BtnDown()
    {
        this.GetComponent<Image>().color = Color.HSVToRGB(175 / 360f, 42 / 100f, 100 / 100f);
        Destroy(back);
        back = Instantiate(around, new Vector3(0, 0, 0), new Quaternion(0, 0, 0, 0));
        back.transform.SetParent(Canvas.transform);
        back.GetComponent<RectTransform>().position = this.GetComponent<RectTransform>().position;
        back.SetActive(false);
        runTime = 0;
        this.GetComponent<RectTransform>().sizeDelta  = new Vector2(-0.1f * width + 0.9f * size_x, -0.1f * height + 0.9f * size_y);
        animationOn = true;

        Debug.Log(width.ToString() +" " + size_x.ToString());
    }
    public void BtnUp()
    {
        //抬起
        this.GetComponent<Image>().color = Color.HSVToRGB(175 / 360f, 0f / 100f, 100 / 100f);
        this.GetComponent<RectTransform>().sizeDelta  = new Vector2(size_x, size_y);
    }

    void Update()
    {
        if (animationOn)
        {
            runTime += Time.deltaTime;
            back.GetComponent<RectTransform>().sizeDelta = new Vector2((width + size_x) * (1.5f - (0.5f - runTime) * (0.5f - runTime) * 2), (height + size_y) * (1.5f - (0.5f - runTime) * (0.5f - runTime) * 2));
            back.GetComponent<Image>().color = new Color32(148, 255, 245, (byte)((1 - runTime * 2) * 255));
            back.SetActive(true);
            if(runTime >= 0.5f)
            {
                back.SetActive(false);
                animationOn = false;
                Destroy(back);
            }
        }
    }
}
